package com.lam.framework.init;

import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.lam.common.components.data.interceptor.DataInjectScanUtil;
import com.lam.common.components.dict.DictCacheUtils;
import com.lam.common.components.dict.interceptor.DictScanUtil;
import com.lam.framework.async.AsyncManager;
import com.lam.framework.mybatis.MybatisUtil;

/**
 * @author lam
 *
 */
@Component
@Order(1)
public class InitService implements ApplicationListener<ContextRefreshedEvent> {
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private volatile AtomicBoolean isInited = new AtomicBoolean(false);

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		//防止重复触发
        if(!isInited.compareAndSet(false, true)) {
            return;
        }
        start(event);
	}
	
	private void start(ContextRefreshedEvent event) {
		
		MybatisUtil.initIdWorker();
		AsyncManager.init();
		
		DictScanUtil.addTableList(DictCacheUtils.getAllConfigTableName());
		logger.debug("------------- 扫描实体类字典注解开始 -------------");
		DictScanUtil.scanTableList("com.lam.**.entity");
		DictScanUtil.scanTableList("com.lam.**.vo");
		DataInjectScanUtil.scanTableList("com.lam.**.entity");
		DataInjectScanUtil.scanTableList("com.lam.**.vo");
		logger.debug("------------- 扫描实体类字典注解结束 -------------");
    }

}
